package com.yxinsur.imc.business.events.publisher;

import com.yxinsur.imc.business.domain.*;
import com.yxinsur.imc.business.dto.CustomerAddressList;
import com.yxinsur.imc.business.dto.OrderCustomerDto;
import com.yxinsur.imc.business.events.event.OrderCustomerEvent;
import com.yxinsur.imc.business.form.OrderPersonForm;
import com.yxinsur.imc.business.mapper.ImcBrokerMapper;
import com.yxinsur.imc.business.mapper.ImcTeamMapper;
import com.yxinsur.imc.business.mapper.ImcTenantMapper;
import com.yxinsur.imc.common.core.domain.entity.SysOrg;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.system.mapper.SysOrgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class OrderCustomerEventPublisher {
    @Autowired
    private ImcBrokerMapper brokerMapper;
    @Autowired
    private ImcTeamMapper teamMapper;
    @Autowired
    private SysOrgMapper orgMapper;
    @Autowired
    private ImcTenantMapper tenantMapper;


    @Autowired
    private ApplicationContext applicationContext;

    public void pushlish(String msg,Long brokerId, List<OrderPersonForm> personList){
        List<OrderCustomerDto> customerDtoList = makeCustomerDtoList(brokerId,personList);
        for (OrderCustomerDto customerDto : customerDtoList) {
            applicationContext.publishEvent(new OrderCustomerEvent(this, msg,customerDto));
        }

    }

    private List<OrderCustomerDto> makeCustomerDtoList(Long brokerId,List<OrderPersonForm> personList) {
        ImcBroker imcBroker = brokerMapper.selectById(brokerId);
        String brokerGuid = imcBroker.getBrokerGuid();
        ImcTeam team = teamMapper.selectImcTeamById(imcBroker.getTeamId());
        SysOrg sysOrg = orgMapper.selectOrgById(team.getOrgId());
        String orgGuid = sysOrg.getOrgGuid();
        ImcTenant imcTenant = tenantMapper.selectImcTenantById(sysOrg.getTenantId());
        String tenantGuid = imcTenant.getTenantGuid();
        ArrayList<OrderCustomerDto> customerDtos = new ArrayList<>();
        for (OrderPersonForm person : personList) {
            //姓名 电话号码  姓名  身份证号
            String fullName = person.getFullName();
            String mobile = person.getMobile();
            String certiCode = person.getCertiCode();
            if(StringUtils.isEmpty(fullName)){
                continue;
            }
            if(StringUtils.isEmpty(mobile) && StringUtils.isEmpty(certiCode)){
                continue;
            }
            OrderCustomerDto customerDto = new OrderCustomerDto();
            customerDto.setAgentUserId(brokerGuid);
            customerDto.setAgentOrgNo(orgGuid);
            customerDto.setAgentChannelNo(tenantGuid);
            customerDto.setCustomerName(person.getFullName());
            customerDto.setGender(person.getGender().equals("0")?"M":"F");
            customerDto.setHeight(person.getHeight());
            customerDto.setWeight(person.getWeight());
            customerDto.setBirthday(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,person.getBirthday()));
            customerDto.setMobile(person.getMobile());
            customerDto.setEmail(person.getEmail());
            customerDto.setCardTypeCode(person.getCertiType().equals("身份证")?"01":"00");
            customerDto.setCardTypeName(person.getCertiType());
            customerDto.setCardNo(person.getCertiCode());
            customerDto.setCardEndDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,person.getCertiExpiryDate()));
            String maritalStatus = person.getMaritalStatus();
            if("未婚".equals(maritalStatus)){
                maritalStatus="01";
            }else if("已婚".equals(maritalStatus)){
                maritalStatus="02";
            }else if("离异".equals(maritalStatus)){
                maritalStatus="03";
            }else if("丧偶".equals(maritalStatus)){
                maritalStatus="04";
            }else{
                maritalStatus="05";
            }
            customerDto.setMaritalStatus(maritalStatus);//婚姻
            customerDto.setAnnualIncome(String.valueOf(person.getIncome()));
            ArrayList<CustomerAddressList> customerAddressLists = new ArrayList<>();
            CustomerAddressList customerAddressList = new CustomerAddressList();
            customerAddressList.setProvinceName(person.getProvince());
            customerAddressList.setCityName(person.getCity());
            customerAddressList.setAreaName(person.getCounty());
            customerAddressList.setAddress(person.getHomeAddr());
            customerAddressLists.add(customerAddressList);
            customerDto.setCustomerAddressList(customerAddressLists);

            customerDto.setWorkUnitName(person.getWorkUnit());//
            customerDto.setBankName(person.getBankName());
            // "bankName":"湖北省洪山区区中南支行",
            customerDto.setBankCardNo(person.getAccount());
            // "bankCardNo":"122222222",
            customerDto.setCreateId("寿险系统");
            customerDto.setUpdateId("寿险系统");
            // "updateId":"鲸云保",
            customerDtos.add(customerDto);

        }
        return customerDtos;
    }
}
